package androidawn.framework.model;

import java.util.ArrayList;
import java.util.List;

public class ModeBase implements IPropertyChangedNotify {

	private List<IPropertyChangedListener> listeners = new ArrayList<IPropertyChangedListener>();
	
	public int modelId;

	public ModeBase(int modelId)
	{	
		ModelLocator.getInstance().register(modelId, this);
	}
	
	@Override
	public void addPropertyChangedListener(IPropertyChangedListener listener) {
		if(listeners.contains( listener))
		{
			return;
		}
		listeners.add(listener);
	}

	@Override
	public void removePropertyChangedListener(IPropertyChangedListener listener) {
		listeners.remove(listener);
	}
	
	protected final void notifyPropertyChanged(String property, Object newValue, Object oldValue)
	{
		for(IPropertyChangedListener listener : listeners)
		{
			listener.onPropertyChanged(property, newValue, oldValue);
		}
	}
	
	public int getModelId() {
		return modelId;
	}
	
	public void Dispose()
	{
		ModelLocator.getInstance().unRegister(modelId, this);
	}

}
